﻿using System.Web;
using OpenWaves.ActivityFeed.NHibernate;
using NHibernate;

namespace OpenWaves.ActivityFeed.Web.NHibernate
{
    public class NHibernateSessionPerRequestProvider : INHibernateSessionProvider
    {
        public ISession Session
        {
            get { return CurrentSession; }
        }

        private static readonly string SessionRequestKeyName = "Session" + typeof(NHibernateSessionPerRequestProvider).FullName;
        private static readonly string TransactionRequestKeyName = "Transaction" + typeof(NHibernateSessionPerRequestProvider).FullName;

        public static ISession CurrentSession
        {
            get{return HttpContext.Current.Items[SessionRequestKeyName] as ISession;}
            set { HttpContext.Current.Items[SessionRequestKeyName] = value; }
        }

        public static ITransaction CurrentTransaction
        {
            get { return HttpContext.Current.Items[TransactionRequestKeyName] as ITransaction; }
            set { HttpContext.Current.Items[TransactionRequestKeyName] = value; }
        }
    }
}